import Bezier from 'bezier';
// import {ParticleSystem} from "../../../creator";

var fishfactory = require('../module/fishfactory')();
var GameData = require('../module/gameData')();
var catStage = cc.Enum({
    chuishui: 0,//出水状态
    jingtai: 1,//静态状态
    rushui: 2,// 入水状态
    sheji: 3,//射击状态  （美术做错了  错误状态）
    shejishenti: 4,// 射击状态
    touzhi: 5,// 投掷状态（用于投放道具时候）
});
var that;
cc.Class({

    extends: cc.Component,
    properties: {

        coinNum: cc.Label, /* 金币数 */

        cat: dragonBones.ArmatureDisplay, /* 猫 */
        cat_State: null,
        // gun: dragonBones.ArmatureDisplay, /*手枪骨骼*/
        gun: cc.Node, /*手枪骨骼*/
        gun_State: null,

        hand: cc.Node,

        isPause: false, /* 游戏暂停 */

        /* 鱼 */

        fishprefab: {
            default: [],
            type: cc.Prefab
        },

        // Allfishprefab: cc.Prefab, /* 各种鱼的纹理集 */

        fishArr: [], /* 存储所以显示在屏幕上的鱼 */
        catchFish: [], /*存储被抓捕的鱼*/
        bulletfab: cc.Prefab, /* 子弹 */
        bulletAtlas: cc.SpriteAtlas, /* 所有子弹的图集 */
        bullet: cc.Sprite,
        bulletArr1: [],
        bulletArr2: [], /*用于存储子弹发射出去的*/
        bulletPool: null,
        bulletType: 1, /*子弹的类型*/
        point1: cc.Sprite, /*氧气瓶上的线开始点*/
        point2: cc.Sprite, /*枪上的线开始点*/
        line: cc.Graphics, /* 声明一个 Graphics（画线）的组件 */

        /* 氧气瓶的数据 */
        lifeAll: cc.Label,
        lifeNum: cc.Label,
        life_Allnum: 0,
        life_Num: 0, /* 多少次生命 */
        /* 绳子的数据 */


        ropeLen: 600, /* 子弹可以打到的距离 */
        _dt: 0,
        isClick: true,
        isStartClick: false,
        evtpoint: cc.Sprite,

        ceshitime: 0,
        gameWave: 1, /*游戏的波数*/
        fristIdArr: [], /*记录第一波产生哪些id的鱼  然后在捕捉到鱼时 创建新鱼时会根据这些鱼id创建新鱼*/
        rope11: {
            default: [],
            type: cc.Node
        },

        /*是否点击道具*/
        isdaojuClick: 0,
        fang_daoju: true,
        particle1: cc.ParticleSystem,
        particle2: cc.ParticleSystem,
        ropePoint: cc.Prefab, /*回来轨迹上的节点*/
        drawNode: cc.Prefab,
        isUserboom: false,
        isArrive: false,
        wang: cc.Sprite,
        ping: cc.Sprite,
        top: cc.Node,
        addcoin: cc.Prefab, /*是玩家打到鱼得到金币的特效*/
        qipao: cc.Prefab,
        boom: cc.Prefab,

        comboCount: 0,
        bg: cc.Node,
        contagion: cc.Prefab,

        moveMusic: {
            default: [],
            type: [cc.AudioClip]
        },
        select1: cc.Sprite,
        select2: cc.Sprite,

        // ceshifish: cc.Sprite,
    },
    /*
        spriteFrame.getTexture().setAntiAliasTexParameters();
        不抗锯齿用
        spriteFrame.getTexture().setAliasTexParameters();
    */
    onLoad() {
        GameData.isPause = true;

        that = this;
        GameData.GameLayer = this;
        this.click = false;
        this.getlocakdata = GameData.getlocalData("fishData");
        this.coinNum.string = this.getlocakdata.coinNum;

        /*初始化氧气瓶的容量*/
        let _idx = this.getlocakdata.upgrade.up_zidan[4] - 1;
        this.life_Allnum = GameData.upgradeData.zidan[_idx].bullet_num;
        this.life_Num = GameData.upgradeData.zidan[_idx].bullet_num;
        this.lifeAll.string = this.life_Allnum + "";
        this.lifeNum.string = this.life_Num + "";
        this.gamebegin = false;
        // console.log(this.getlocakdata);
        // cc.view.enableRetina(true);
        // cc.view.resizeWithBrowserSize(true);
        cc.find("bg").active = true;
        cc.find("bg").runAction(
            cc.sequence(
                cc.fadeOut(1),
                cc.callFunc(function () {
                    that.gamestart();
                    that.bg.active = false;
                })
            )
        )
        GameData.enter();
        var id = GameData.playMusic(this.moveMusic[0], true);
        cc.audioEngine.setVolume(id, 0.2);
    },
    onEnable() {
    },
    gamestart() {
        this.cat.node.zIndex = 2;
        this.cat.node.runAction(
            cc.sequence(
                cc.moveTo(1.5, -352, -102),
                cc.callFunc(function () {
                    GameData.playEffect(that.moveMusic[5]);
                    for (var a = 0; a < 20; a++) {
                        var qipao = cc.instantiate(that.qipao);
                        qipao.y = -130;
                        qipao.x = -352;
                        qipao.parent = that.node;
                    }
                    that.cat.playAnimation("LX", 1).timeScale = 2;
                }),
                cc.delayTime(0.45),
                cc.callFunc(function () {
                    that.cat.playAnimation("jingtai", 1);
                    that.gameinit();
                })
            )
        )
    },
    gameinit() {
        this.setSp(1);
        this.gamebegin = true;
        this.wang.node.zIndex = 2;
        this.ping.node.zIndex = 2;
        this.gun.zIndex = 2;
        this.hand.zIndex = 3;
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouch, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouch, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouch, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouch, this);
        this.cat.addEventListener(dragonBones.EventObject.LOOP_COMPLETE, this.catPlay, this);
        // this.gun.addEventListener(dragonBones.EventObject.LOOP_COMPLETE, this.gunPlay, this);
        GameData.isPause = false;
        this.initGameData();
    },
    setSp(bool) {
        if (bool) {
            this.wang.node.active = true;
            this.ping.node.active = true;
            this.gun.active = true;
        } else {
            this.wang.node.active = false;
            this.ping.node.active = false;
            this.gun.active = false;
        }
    },
    /* 初始化游戏的数据 */
    initGameData() {
        this.cost = 1;
        /*默认每打出一发子弹消耗1生命*/
        this.catchFish = [];
        this.isUserboom = false;
        this.isUseryuer = false;
        this.isdaojuClick = 0;
        GameData.is_check = false;
        this.isArrive = true;
        /*表示打出的绳子是否回到固定点 用于最后判断是否进入计算界面*/

        /*子弹与鱼的碰撞检测关闭*/
        this.initwave();
        this.fishArr = [];
        /*子弹的回收池*/
        this.bulletArr1 = [];
        GameData.overData = [];
        /*将全局变量重置为null*/
        this.fristIdArr = [[], []];
        this.gameWave = 0;

        /*初始化枪的攻击力*/
        let attackidx = this.getlocakdata.upgrade.up_zidan[0];
        GameData.attack = GameData.getData("attack", attackidx - 1);
        /*初始化子弹的攻击力*/
        this.unlocklevel = this.getlocakdata.unlockFish;
        /*获取玩家解锁最大等级的鱼*/
        this.initFish();
        /*初始化鱼*/
        fishfactory.bezierArr = [];
        /*重置为空数组 防止再玩一次时数组还有数据*/
        let _drawNode = cc.instantiate(this.drawNode);
        _drawNode.parent = this.node;
        this.rope = _drawNode.getComponent(cc.Graphics);
        // this.rope.lin
        this.rope.lineWidth = 5;
        this.rope.strokeColor = cc.hexToColor('#8B6363');
        this.rope.moveTo(0, 0);
        this.rope.lineTo(0, 0, 0, 200);
        this.rope.stroke();

        var rope_idx = this.getlocakdata.upgrade.up_zidan[3];
        this.ropeLen = GameData.upgradeData.zidan[rope_idx - 1].rope_len * 600;


        this.isClick = true;
        /*是否可以点击*/

        this.bulletPool = new cc.NodePool();

        this.bulletType = 1;
        /*子弹类型*/

        this.daojupool1 = new cc.NodePool();
        this.daojupool2 = new cc.NodePool();

        // this.ropeLen = 600;
        /*绳子的初始长度*/

        /*创建子弹*/

        this.ropePool = new cc.NodePool();
        this.ropeArr = [];

        /*初始化氧气瓶*/


        /*当生命数低于5时开始加载home场景中的资源*/
        this.loaded = false;


        /*添加气泡*/
        // this.addqipao();
    },
    addqipao() {
        for (var a = 0; a < 10; a++) {
            var qipao = cc.instantiate(this.qipao);
            var _x = Math.floor(Math.random() * 1136 - 568);
            var _y = Math.floor(Math.random() * 421 - 200);
            qipao.y = _y;
            qipao.x = _x;
            qipao.parent = this.node;
        }
    },
    /*
    * desc: 绳子回来动作
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/12/10
    */
    ropeBackDraw() {
        for (var a = 0; a < this.ropeArr.length; a++) {
            var that = this;
            var fun = (function (ropeArr) {
                let _rope = ropeArr.rope;
                let _draw = ropeArr.draw;
                let _obj = ropeArr.obj;
                ropeArr.rope.clear();
                var Arr = [];
                var pos = _obj.getChildByName("point");

                var pos11 = pos.parent.convertToWorldSpaceAR(pos.getPosition());
                var pos2 = that.convertToNodeSpaceAR(that.node, pos11);

                var bezier1 = [cc.v2(that.point1.node.x, that.point1.node.y),
                    cc.v2(_draw[0].x, _draw[0].y),
                    cc.v2(_draw[1].x, _draw[1].y),
                    cc.v2(_draw[2].x, _draw[2].y),
                    cc.v2(_draw[3].x, _draw[3].y),
                    cc.v2(pos2.x, pos2.y),
                ];
                var moveArr = Bezier(bezier1, 100, 1);
                for (var a = 0; a < 1; a += 0.001) {
                    let position1 = moveArr.getPoint(a);
                    Arr.push(position1);
                }
                var clickfish = _obj.children;
                // for (var a = 0; a < clickfish.length; a++) {
                //     if (clickfish[a].name == "Allfish") {
                //         if (clickfish[a].clickPos) {
                //             var p = that.getY(Arr, clickfish[a].clickPos.x);
                //             var _y = p.y;
                //             var pos = that.node.convertToWorldSpaceAR(p);
                //             var f2 = _obj.convertToNodeSpaceAR(pos);
                //             clickfish[a].x = f2.x;
                //             clickfish[a].y = f2.y;
                //             if (clickfish[a].x >= 50) {
                //                 clickfish[a].clickPos = null;
                //                 clickfish[a].isgo = false;
                //                 clickfish[a].position = _obj.getChildByName("end").getPosition();
                //             }
                //         }
                //     }
                // }
                for (var a = 0; a < Arr.length - 1; a++) {
                    that.drawline(_rope, Arr[a], Arr[a + 1]);
                }
            })(this.ropeArr[a]);
        }
    },
    drawline(rope, pos1, pos2) {
        rope.lineWidth = 5;
        rope.strokeColor = cc.color(0, 0, 0, 50);
        // cc.hexToColor('#ffffff');
        rope.moveTo(pos1.x, pos1.y);
        rope.lineTo(pos2.x, pos2.y);
        rope.close();
        rope.stroke();
    },
    /*
    * desc:  通过x值获取y值
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/12/17
    */
    getY(Arr, x) {
        var _min = null;
        var min_idx = null;
        for (var a = 0; a < Arr.length; a++) {
            var _d = Math.abs(x - Arr[a].x);
            if (_min) {
                if (_min > _d) {
                    _min = _d;
                    min_idx = a;
                }
            } else {
                _min = _d
            }
        }
        return Arr[min_idx]
    },
    /*
    * desc: 获取两点之间的中点坐标 通用工具方法
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/12/10
    */
    getzhongxian(pos1, pos2, z_h, type) {
        let _jiaodu1 = this.getangle(pos1, pos2);
        let _dis = Math.sqrt(Math.pow((pos2.x - pos1.x), 2) + Math.pow((pos2.y - pos1.y), 2));
        let _hudu1 = Math.atan(z_h * 2 / _dis);
        let xiebian = z_h / Math.sin(_hudu1);
        let _jiaodu2 = Math.abs(_hudu1 * 180 / 3.14);
        let _jiaodu;
        switch (type) {
            case 1:
                _jiaodu = _jiaodu1 + _jiaodu2;
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                _jiaodu = _jiaodu1 - _jiaodu2;
                break;
        }
        let _hudu = _jiaodu * 3.14 / 180;
        let _x = xiebian * Math.cos(_hudu);
        let _y = xiebian * Math.sin(_hudu);
        return cc.v2(pos1.x + _x, pos1.y + _y);
    },
    /*
    * desc: 创建子弹
    * author: <zhouzhiwei>
    * @param: type 子弹的类型
    * date: 2018/11/15
    */
    CreateBullet1(type) {
        let _bullet;
        if (this.bulletPool.size() > 0) {
            _bullet = this.bulletPool.get();
        } else {
            _bullet = cc.instantiate(this.bulletfab);
        }
        _bullet.getComponent(cc.Sprite).spriteFrame = this.bulletAtlas.getSpriteFrame("bullet" + type);

        _bullet.opacity = 255;
        _bullet.parent = this.gun;
        this.gun.getChildByName("gun1").zIndex = 10;
        _bullet.setPosition(60, 10);
        _bullet.rotation = 0;
        _bullet.is_check = false;


        // _bullet.angle = 0;

        _bullet.idx = this.bulletArr1.length - 1;
        _bullet.endPos = null;
        return _bullet;
    },
    CreateBullet(type, pos, _r) {
        this.gun.getChildByName("gun1").zIndex = 10;
        let _bullet;
        if (this.bulletPool.size() > 0) {
            _bullet = this.bulletPool.get();
        } else {
            _bullet = cc.instantiate(this.bulletfab);
        }
        _bullet.getComponent(cc.Sprite).spriteFrame = this.bulletAtlas.getSpriteFrame("bullet" + type);
        _bullet.parent = this.node;
        _bullet.opacity = 255;
        _bullet.position = pos;
        _bullet.rotation = _r;
        _bullet.is_check = true;
        _bullet.idx = this.bulletArr1.length - 1;
        _bullet.endPos = 2;
        this.bulletArr1.push(_bullet);
    },
    onTouch(evt) {
        var pos2 = this.convertToNodeSpaceAR(this.node, evt.getLocation());
        let _angle1 = this.getangle(this.gun.getPosition(), pos2);
        let _angle = this.getxianxian(this.gun.getPosition(), pos2, _angle1);
        if (this.isClick) {
            switch (evt.type) {
                case "touchstart":
                    this.isStartClick = true;

                    if (this.isdaojuClick > 0) {
                        cc.find("right_ui/daoju").getComponent("daoju").playAction(this.isdaojuClick);
                        cc.find("right_ui/daoju").getComponent("daoju").coinUpdate();
                        this.gun.active = false;
                        this.hand.active = true;
                    } else {
                        if (this.life_Num > 0) {
                            this.changeLife(-this.cost);
                            this.isStartClick = true;
                            this.onTouchStart(evt, _angle);
                            this.gun.active = true;
                            this.hand.active = false;
                        } else {
                            this.isStartClick = false;
                        }

                    }

                    break;
                case "touchmove":
                    if (this.isStartClick) {
                        this.onTouchMove(evt, _angle);
                    }
                    break;
                case "touchend":
                    if (this.isStartClick) {
                        this.onTouchEnd(evt, _angle);
                    }
                    break;
                case "touchcancel":
                    if (this.isStartClick) {
                        this.onTouchCancel(evt, _angle);
                    }
                    break;
            }
        } else {
            this.isStartClick = false;
        }
    },
    onTouchStart(evt, _angle) {
        var pos2 = this.convertToNodeSpaceAR(this.node, evt.getLocation());
        if (this.isdaojuClick == 0) {
            this.changeCat("shejijingzhi");
            this.isStartClick = true;
            GameData.is_check = true;
            // this.showEvtpoint(true, pos2.x, pos2.y, "point");
            this.rotationBullet(_angle);
        } else {
            this.hand.getChildByName("daoju").active = true;
            this.hand.getChildByName("daoju").getComponent(cc.Sprite).spriteFrame = this.bulletAtlas.getSpriteFrame("daojusp1");
            if (this.isdaojuClick == 2) {
                this.hand.getChildByName("daoju").getComponent(cc.Sprite).spriteFrame = this.bulletAtlas.getSpriteFrame("daojusp2")
            }
            this.changeCat("shejijingzhi");
            this.hand.rotation = _angle;
            // this.hand.angle = -_angle;
        }
    },
    onTouchCancel(evt, _angle) {
        this.onTouchEnd(evt, _angle);
    },
    onTouchMove(evt, _angle) {
        var pos2 = this.convertToNodeSpaceAR(this.node, evt.getLocation());
        if (this.isdaojuClick == 0) {
            this.showEvtpoint(true, pos2.x, pos2.y, "point");
            this.rotationBullet(_angle);
        } else {
            this.hand.getChildByName("daoju").active = true;
            this.hand.getChildByName("daoju").getComponent(cc.Sprite).spriteFrame = this.bulletAtlas.getSpriteFrame("daojusp1");
            if (this.isdaojuClick == 2) {
                this.hand.getChildByName("daoju").getComponent(cc.Sprite).spriteFrame = this.bulletAtlas.getSpriteFrame("daojusp2")
            }
            this.hand.rotation = _angle;
            // this.hand.angle = -_angle;
            this.showEvtpoint(true, pos2.x, pos2.y, "daojufanwei");
        }
    },
    onTouchEnd(evt, num, _angle) {
        if (this.isdaojuClick == 0) {
            if (this.isStartClick) {
                // let _rota = this.bulletArr1[0].parent.angle * 3.14 / 180;
                var pos11 = this.bullet.node.parent.convertToWorldSpaceAR(this.bullet.node.getPosition());
                var _pos1 = this.node.convertToNodeSpaceAR(pos11);
                var _rotation = this.bullet.node.parent.rotation;
                this.bullet.node.active = false;
                if (this.bulletType == 1) {/*判断是第一中子弹还是第二中子弹*/
                    let _rota = -_rotation * 3.14 / 180;
                    var _x = this.ropeLen * Math.cos(_rota) + this.gun.x;
                    var _y = this.ropeLen * Math.sin(_rota) + this.gun.y;
                    if (_angle > 0) {
                        _y = -y;
                    }
                    let pos3 = cc.v2(_x, _y);
                    this.CreateBullet(this.bulletType, _pos1, _rotation);
                    this.launch_bullet(this.bulletArr1[0], pos3);

                } else {
                    var roatArr = [_rotation - 15, _rotation, _rotation + 15];
                    for (var a = 0; a < 3; a++) {
                        this.CreateBullet(this.bulletType, _pos1, roatArr[a]);
                        let _rota = -roatArr[a] * 3.14 / 180;
                        var _x = this.ropeLen * Math.cos(_rota) + this.gun.x;
                        var _y = this.ropeLen * Math.sin(_rota) + this.gun.y;
                        if (_angle > 0) {
                            _y = -y;
                        }
                        let pos3 = cc.v2(_x, _y);
                        this.launch_bullet(this.bulletArr1[a], pos3);
                    }
                }

                this.isClick = false;
                // this.changeCat("shejishenti");
                /*更改小猫的射击状态*/
                this.changeCat();
                this.changeGun();
                this.showEvtpoint(false, null, null, "point");
                GameData.is_check = true;
            }

            /*子弹与鱼的碰撞检测关闭*/
        } else if (this.isdaojuClick > 0) {
            this.showEvtpoint(false, null, null);
            var pos2 = this.convertToNodeSpaceAR(this.node, evt.getLocation());
            var _p2 = this.hand.getChildByName("daoju").parent.convertToWorldSpaceAR(this.hand.getChildByName("daoju").getPosition());
            /*将point2的坐标转换成屏幕坐标*/
            var _point2 = this.node.convertToNodeSpaceAR(_p2);
            let pos22 = this.getzhongxian(_point2, pos2, 30, 1);

            let point = [_point2, pos22, pos2];
            var action = cc.cardinalSplineTo(0.5, point, 0);

            var that = this;
            this.changeHand();
            if (this.isdaojuClick == 1) {
                this.particle1.node.active = true;
                this.particle1.node.x = _point2.x;
                this.particle1.node.y = _point2.y;
                this.particle1.node.runAction(cc.sequence(
                    cc.callFunc(function () {
                        that.particle1.resetSystem();
                        /*DBZ XBZ*/
                        that.particle1.node.getChildByName("daoju").getComponent(dragonBones.ArmatureDisplay).playAnimation("XBZ", 0)
                    }),
                    action,
                    cc.callFunc(function () {
                        // that.particle.node.active = false;
                        that.isUserboom = true;
                        that.particle1.stopSystem();
                        that.isuseDaoju = true;
                        that.particle1.node.getChildByName("daoju").getComponent(dragonBones.ArmatureDisplay).playAnimation("BZ", 1)
                    }),
                    cc.delayTime(2),
                    cc.callFunc(function () {
                        that.particle1.node.active = false;
                        that.isUserboom = false;
                    })
                ));
            } else {
                this.particle2.node.active = true;
                this.particle2.node.x = _point2.x;
                this.particle2.node.y = _point2.y;
                this.particle2.node.runAction(cc.sequence(
                    cc.scaleTo(0.01, 1, 1),
                    cc.callFunc(function () {
                        that.particle2.resetSystem();
                    }),
                    action,
                    cc.callFunc(function () {
                        that.isUseryuer = true;
                        // that.useDaoju1();
                    }),
                    cc.spawn(
                        cc.moveBy(3, 0, -200),
                        cc.scaleTo(3, 0, 0)
                    ),
                    cc.callFunc(function () {
                        that.particle2.stopSystem();
                        that.isUseryuer = false;
                        that.particle2.node.active = false;
                    }),
                ));
            }
            this.isdaojuClick = 0;
        }
    },
    useDaoju() {
        var pos = this.particle1.node.getPosition();
        for (var a = 0; a < this.fishArr.length; a++) {
            var _d = this.getDistance(this.fishArr[a].getPosition(), pos);
            if (_d <= 200) {
                this.fishArr[a].moveStop = true;
                this.fishArr[a].ischarged = true;
                if (this.fishArr[a].playXY) {
                    this.fishArr[a].playXY = false;
                    this.fishArr[a].getChildByName("Allfish").getChildByName("fishtype").getChildByName("fish").getComponent(dragonBones.ArmatureDisplay).playAnimation("XY", 0)
                }
            }
        }
    },
    useDaoju1() {
        var pos = this.particle2.node.getPosition();
        for (var a = 0; a < this.fishArr.length; a++) {
            var _id = this.fishArr[a].id;

            if (!this.fishArr[a].iseat && _id != 12 && _id != 13 && _id != 14 && _id != 9) {
                var _d = this.getDistance(this.fishArr[a].getPosition(), pos);
                if (_d <= 200) {
                    // console.log("ssss", _id, _d);
                    var obj = this.fishArr[a];
                    var end_p = this.fishArr[a].getPosition();
                    /*当前鱼的位置*/
                    var start_p = this.particle2.node.getPosition();
                    this.restAngle(obj, end_p, start_p);
                    obj.moveStop = true;
                    obj.backMove = false;
                    obj.eatback = false;
                    var fun = (function (num) {
                        return function () {
                            num.eatMove = true;
                            // console.log("obj.id", num.id);
                            return num;
                        };
                    })(obj);
                    obj.iseat = true;
                    obj.runAction(cc.sequence(
                        cc.moveTo(0.5, start_p.x, start_p.y),
                        cc.callFunc(fun)
                    ));
                    obj.end_p = this.fishArr[a].getPosition();

                }
            }

        }
    },
    changeHand() {
        var that = this;
        this.hand.getChildByName("daoju").active = false;
        this.hand.runAction(
            cc.sequence(
                cc.rotateTo(0.3, 0),
                cc.callFunc(function () {
                    that.gun.active = true;
                    that.changeCat("jingtai");
                })
            )
        );
    },
    changeGun() {
        this.gun.runAction(
            cc.sequence(
                cc.rotateBy(0.2, -30),
                cc.rotateTo(0.2, 50),
                cc.callFunc(function () {
                    that.gun.rotation = 0;
                    for (var a = 0; a < that.bulletArr1.length; a++) {
                        that.bulletArr1[a].endPos = 1
                    }
                    // that.gun.angle = 0;
                    // that.bulletArr1[0].endPos = 1;
                })
            )
        );
        var that = this;
        this.cat.node.runAction(
            cc.sequence(
                cc.rotateBy(0.2, 20),
                cc.rotateTo(0.2, 0),
                cc.callFunc(function () {
                    that.changeCat("jingtai");

                })
            )
        )
    },
    /*
    * desc: 播放不同状态下小猫的动画
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/11/12
    */
    changeCat(state) {
        /*
        * desc:小猫状态
        * chuishui 出水状态
        * jingtai 静态状态
        * rushui  入水状态
        * sheji   射击状态  （美术做错了  错误状态）
        * shejishenti  射击状态
        * shejijingzhi  射击静止状态
        * touzhi   投掷状态（用于投放道具时候）
        * author: <zhouzhiwei>
        * @param:
        * date: 2018/11/12
        */

        this.cat_State = state;
        this.cat.playAnimation(state, 1);

    },
    /*ss
    * desc: 监听小猫的动画
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/11/12
    */
    catPlay() {
        switch (this.cat_State) {
            case "chuishui":
                this.changeCat("jingtai", 1);
                break;
            case "jingtai":
                this.changeCat("jingtai", 1);
                break;
            case "rushui":
                this.changeCat("jingtai", 1);
                break;
            case "shejishenti":
                // that.cat.node.rotation = 0;
                // this.changeCat("jingtai", 1);
                break;
            case "touzhi":
                this.changeCat("jingtai", 1);
                break;
        }
    },
    /*
    * desc:  是否显示鼠标点击的对应点
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/11/12
    */
    showEvtpoint(_bool, x, y, type) {
        if (_bool) {
            this.evtpoint.node.active = true;
            this.evtpoint.node.getComponent("cc.Sprite").spriteFrame = this.bulletAtlas.getSpriteFrame(type);
            this.evtpoint.node.x = x;
            this.evtpoint.node.y = y;
        } else {
            this.evtpoint.node.active = false;
        }
    },
    /* 初始化 生命 的数据 */
    initLifeData() {
        // this.lifeStartLen = this.lifeBg.height;
        // this.lifeLen = this.lifeBg.height / this.lifeNum;
    },
    /**
     *  旋转放射枪
     *
     */
    rotationBullet(angle) {
        this.cat.node.rotation = -angle * 0.5;
        this.gun.rotation = -angle;
        // this.cat.node.angle = angle * 0.5;
        // this.gun.angle = angle;
    },
    /**
     *获取两点的夹角度数
     *
     * @param {*} pos1 点一
     * @param {*} pos2 点二
     * @returns 两夹角的度数
     */
    getangle(pos1, pos2) {
        let _tanNum = (pos2.y - pos1.y) / (pos2.x - pos1.x);
        var PI = 3.14;
        return Math.atan(_tanNum) * 180 / PI;
    },
    /*
    * desc:
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/10/22
    */
    launch_bullet(obj, pos) {
        GameData.playEffect(that.moveMusic[2]);
        // console.log("发射子弹");
        // if (this.bulletType == 1) {
        //
        //     this.restBullet(obj);
        // }
        var _idx = this.getlocakdata.upgrade.up_zidan[2];
        var _t1 = GameData.upgradeData.zidan[_idx - 1].f_speed;
        var _t = this.ropeLen / (800 * _t1);

        obj.runAction(cc.sequence(
            cc.moveTo(_t, pos.x, pos.y),
            cc.callFunc(function () {
                /*将鱼和钩子的碰撞检测 关闭*/
                GameData.is_check = false;
                /*回收绳子，执行回收动作*/
                obj.endPos = 3;
                obj.is_check = false;
                that.back_bullet(obj);
            })
        ))
    },
    /*
    * desc: 重置鱼的信息，父节点，位置，重新放入新的容器中，并且从老容器删除。
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/11/15
    */
    restBullet(obj) {
        var pos11 = obj.parent.convertToWorldSpaceAR(obj.getPosition());
        var _pos1 = this.node.convertToNodeSpaceAR(pos11);
        obj.setPosition(_pos1.x, _pos1.y);
        obj.rotation = obj.parent.rotation;
        // obj.angle = obj.parent.angle;
        obj.parent = this.node;

    },

    /*获取两点间的距离*/
    getDistance(pos1, pos2) {
        return Math.sqrt(Math.pow((pos1.x - pos2.x), 2) + Math.pow((pos1.y - pos2.y), 2));
    },
    /*
    * desc: 回收绳子
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/10/22
    */
    back_bullet(obj) {
        obj.opacity = 155;
        obj.is_check = false;
        var Arr = obj.children;
        var _len = obj.children.length;
        for (var a = 0; a < _len; a++) {
            // console.log(Arr.length, a);
            if (Arr[a].name == "Allfish") {
                Arr[a].isgo = false;
            }
        }
        this.rope.clear();
        let _point = obj.getChildByName("point");
        var pos11 = _point.parent.convertToWorldSpaceAR(_point.getPosition());
        var pos22 = this.convertToNodeSpaceAR(this.node, pos11);
        let endPoint = pos22;
        let rota;
        if (endPoint.y > this.point1.node.y) {
            rota = 1;
        } else {
            rota = 4;
        }
        var pos2 = this.getzhongxian(this.point1.node, endPoint, 1, rota);
        var pos1 = this.getzhongxian(this.point1.node, pos2, 1, rota);
        var pos3 = this.getzhongxian(pos2, endPoint, 1, rota);
        let posArr = [pos1, pos2, pos3, endPoint];
        let _Arr = [];
        for (var a = 0; a < 4; a++) {
            let _rope;
            if (this.ropePool.size() > 0) {
                _rope = this.ropePool.get();
            } else {
                _rope = cc.instantiate(this.ropePoint)
            }
            _rope.position = posArr[a];
            _Arr.push(_rope);
        }
        // var rope = this.node.getChildByName("game").getComponent(cc.Graphics);
        var drawnode = cc.instantiate(this.drawNode);
        var rope = drawnode.getComponent(cc.Graphics);
        rope.node.parent = this.node;
        // obj.endPos = 3;
        this.ropeArr[this.ropeArr.length] = {
            rope: rope,
            draw: _Arr,
            obj: obj
        };
        var _d = this.getDistance(this.point1.node, endPoint);

        let _t = [1.8, 1.9, 2, 2.1, 2.2];
        // let _t = [0.8, 0.9, 1, 1.1, 1.2];
        // let _t = [0.2, 0.3, 0.4, 0.5, 0.6];
        let action2;
        for (var a = 0; a < _Arr.length; a++) {
            let pos = this.getzhongxian(this.point1.node, _Arr[a], 30, 4);
            let _s = this.getDistance(_Arr[a], this.point1.node);
            // let _t1 = _t[a] * _s / 300;
            let point = [cc.v2(_Arr[a].x, _Arr[a].y), cc.v2(pos.x, pos.y), cc.v2(this.point1.node.x, this.point1.node.y)];
            var action1 = cc.cardinalSplineTo(_t[a], point, 0);
            // if (a == _Arr.length - 1) {
            //     console.log("sssssss",point,_t[a])
            // }
            _Arr[a].runAction(
                action1
            )
        }
        // let _t = _s / 500;
        // var pos11 = this.point1.node.parent.convertToWorldSpaceAR(this.point1.node.getPosition());
        /*将point1的坐标转换成屏幕坐标*/
        let _s = this.getDistance(endPoint, this.point1.node);
        // let _t1 = _t[_t.length-1] * _s / 300;
        let _t1 = _t[_t.length - 1];
        let pos = this.getzhongxian(this.point1.node, obj, 30, 4);
        let point = [cc.v2(obj.x, obj.y), cc.v2(pos.x, pos.y), cc.v2(this.point1.node.x, this.point1.node.y)];
        var action1 = cc.cardinalSplineTo(_t1, point, 0);
        for (var a = 0; a < that.bulletArr1.length; a++) {
            if (that.bulletArr1[a] == obj) {
                that.bulletArr1.splice(a, 1);
            }
        }
        this.bullet.node.active = true;
        that.isClick = true;
        obj.runAction(
            cc.sequence(
                action1,
                cc.callFunc(function () {
                    /*绳子的 渲染关闭*/
                    /*弹出得到多少金币*/
                    /*将这个飞镖子弹的上所有鱼全部回收*/
                    for (var a = 0; a < obj.children.length; a++) {
                        if (obj.children[a].name == "Allfish") {
                            let idx = null;
                            for (var b = 0; b < GameData.overData.length; b++) {
                                if (GameData.overData[b].id == obj.children[a].id) {
                                    idx = b;
                                    break;
                                }
                            }
                            if (idx != null) {
                                GameData.overData[idx].num += 1;
                                GameData.overData[idx].price = GameData.overData[idx].num * obj.children[a].price
                            } else {
                                if (typeof obj.children[a].id == "number") {
                                    let _fish = {
                                        num: 1,
                                        id: obj.children[a].id,
                                        price: obj.children[a].price
                                    };
                                    GameData.overData.push(_fish);
                                }
                            }


                            that.addcoinAct(-330, 40, obj.children[a].price);
                            obj.parent = null;
                            fishfactory.poolFish(obj.children[a]);
                            obj.children.splice(a, 1);
                            a--;
                            that.CreateNewFish();
                        }
                    }
                    /*将这个飞镖上绑定的所有轨迹点全部回收*/
                    for (var a = 0; a < that.ropeArr.length; a++) {
                        if (that.ropeArr[a].obj == obj) {
                            that.ropeArr[a].rope.clear();
                            that.ropeArr.splice(a, 1);
                            a--;
                        }
                    }
                    if (that.life_Num <= 0) {
                        that.life_Num = -1
                    }
                    /*回收这个子弹飞镖*/
                    that.bulletPool.put(obj);
                })
            )
        )
    },

    pointMove() {

    },
    convertToNodeSpaceAR(node, pos) {
        var pos2 = node.convertToNodeSpaceAR(pos);
        return pos2;
    },
    selectBtn(evt) {
        let _name = evt.currentTarget.name;
        switch (_name) {
            case "btn1":/* 返回 */
                this.goback();
                break;
            case "btn2":/* 首页 */
                cc.director.loadScene("Home");
                break;
            case "btn3":/* 设置 */
                break;
            case "pause":/* ui层上的暂停按钮 */
                this.pause();
                break;
            case "bullt_btn1":/* 更换成子弹1按钮 */
                if (this.gamebegin) {
                    that.updateBullet(1);
                    that.select1.node.active = true;
                    that.select2.node.active = false;
                }

                break;
            case "bullt_btn2":/* 更换成子弹2按钮 */
                if (this.gamebegin) {
                    that.updateBullet(2);
                    that.select1.node.active = false;
                    that.select2.node.active = true;
                }
                break;

        }
    },
    /*
    * desc: 投放道具
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/11/21
    */
    toufangdaoju(type) {
        if (type == 1) {
        } else if (type == 2) {
        }
    },
    /* 绳子 */
    drawrope(x, y, x1, y1, _color) {
        this.rope.lineWidth = 5;
        this.rope.strokeColor = cc.hexToColor(_color);
        this.rope.moveTo(x, y);
        this.rope.lineTo(x1, y1);
        this.rope.close();
        this.rope.stroke();
    },
    /*
    * desc: 更换子弹
    * author: <zhouzhiwei>
    * @param: num:子弹类型
    * date: 2018/11/1
    */
    updateBullet(num) {
        this.bulletType = num;
        this.cost = num;
        /*获取子弹的等级 如果大于3级 */
        if (this.isClick) {
            this.bullet.getComponent(cc.Sprite).spriteFrame = this.bulletAtlas.getSpriteFrame("bullet" + num);
        }
    },
    /*
    *description: 更新位置 绳子的终点位置进行 画线
    *author: <zhouzhiwei>
    *@param:
    *date:
    */
    updateDrawrope() {
        if (this.rope) {
            this.rope.clear();
            for (var a = 0; a < this.bulletArr1.length; a++) {
                if (this.bulletArr1[a].endPos == 2) {
                    var pos11 = this.point2.node.parent.convertToWorldSpaceAR(this.point2.node.getPosition());
                    /*将point2的坐标转换成屏幕坐标*/
                    var _pos1 = this.node.convertToNodeSpaceAR(pos11);
                    /*将point2转换的0屏幕坐标转换成this节点坐标*/
                    let _point = this.bulletArr1[a].getChildByName("point");
                    var pos11 = _point.parent.convertToWorldSpaceAR(_point.getPosition());
                    var _pos2 = this.convertToNodeSpaceAR(this.node, pos11);

                    // var pos22 = this.bulletArr1[a].parent.convertToWorldSpaceAR(this.bulletArr1[a].getPosition());
                    // /*将bullet的坐标转换成屏幕坐标*/
                    // var _pos2 = this.node.convertToNodeSpaceAR(pos22);
                    this.drawrope(_pos1.x, _pos1.y, _pos2.x, _pos2.y, '#000000');
                } else if (this.bulletArr1[a].endPos == 1) {
                    var _pos1 = this.point1.node.getPosition();
                    var pos22 = this.bulletArr1[a].parent.convertToWorldSpaceAR(this.bulletArr1[a].getPosition());
                    /*将bullet的坐标转换成屏幕坐标*/
                    var _pos2 = this.node.convertToNodeSpaceAR(pos22);
                    this.drawrope(_pos1.x, _pos1.y, _pos2.x, _pos2.y, '#000000');
                }
            }
        }
    },
    /*
    * desc: 根据不同数值进行 扣除血量
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/11/23
    */
    changeLife(num) {
        this.life_Num += num;
        this.lifeNum.string = this.life_Num + '';
        if (this.life_Num < 0) {
            this.life_Num = 0;
            this.lifeNum.string = this.life_Num + '';
            this.isClick = false;
            // this.gameOver();
            return;
        }
        if (this.life_Num < 5 && !this.loaded) {
            cc.director.preloadScene("Home", function () {
                that.loaded = true;
            });
        }
    },
    gameOver() {
        this.node.off(cc.Node.EventType.TOUCH_START, this.onTouch, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouch, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.onTouch, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouch, this);
        // GameData.stopMusic();
        GameData.isPause = true;
        this.isClick = false;
        for (var a = 0; a < this.ropeArr.length; a++) {
            this.ropeArr[a].rope.clear();
        }
        for (var a = 0; a < this.bulletArr1.length; a++) {
            this.bulletArr1[a].stopAllActions();
        }
        // GameData.Allfish_pool.clear();
        GameData.fish_Pool.clear();
        this.bulletPool.clear();
        this.daojupool2.clear();
        this.daojupool1.clear();
        this.ropePool.clear();
        this.setSp(null);
        this.cat.playAnimation("CS", 1);

        this.cat.node.runAction(
            cc.sequence(
                cc.moveBy(1, 0, 600),
                cc.callFunc(function () {
                    cc.find("gameOver").active = true;
                    cc.find("gameOver").zIndex = 100;
                })
            )
        )
    },
    goback() {
        GameData.resumeMusic();
        cc.find("BgLayer").active = false;
        cc.find("pauseLayer").active = false;
        GameData.isPause = false;
    },
    pause() {
        GameData.pauseMusic();
        GameData.isPause = true;
        cc.find("BgLayer").active = true;
        cc.find("pauseLayer").active = true;
        cc.find("BgLayer").zIndex = 100;
        cc.find("pauseLayer").zIndex = 100;
    },
    update(dt) {
        GameData.playTime += dt;
        if (!GameData.isPause) {
            /* 随着炮口的旋转  重新绘制不同的发射路线 */
            this.updateDrawrope();
            this.AllfishMove(dt);
            this.ropeBackDraw();
            this.Aifish();
            if (this.isUseryuer) {
                this.useDaoju1();
            } else if (this.isUserboom) {
                this.useDaoju();
            } else {
                for (var a = 0; a < this.fishArr.length; a++) {
                    if (!this.fishArr[a].playXY) {
                        this.fishArr[a].playXY = true;
                        if (this.fishArr[a].id != 12 && this.fishArr[a].id != 13 && this.fishArr[a].id != 14) {
                            this.fishArr[a].getChildByName("Allfish").getChildByName("fishtype").getChildByName("fish").getComponent(dragonBones.ArmatureDisplay).playAnimation("YD", 0);
                        }
                    }
                    if (this.fishArr[a].moveStop && this.fishArr[a].ischarged) {
                        this.fishArr[a].moveStop = false;
                        this.fishArr[a].ischarged = false;
                    }
                }
            }
            if (this.life_Num < 0 && this.ropeArr.length == 0) {
                this.gameOver();
                this.isArrive = false;
            }
        }

    },
    /*
    * desc: 捣乱鱼吃鱼
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/12/12
    */

    Aifish() {
        if (this.catchFish.length > 0) {
            var ischeck = false;
            var Arr = [];
            for (var a = 0; a < this.fishArr.length; a++) {
                if ((this.fishArr[a].id == 7 || this.fishArr[a].id == 8) && !this.fishArr[a].id.moveStop) {
                    Arr.push(this.fishArr[a]);
                    ischeck = true;
                }
            }
            if (ischeck) {
                let minjuli = 150;
                /*设置被抓到的鱼与猫的距离为50像素以内时判定不能在被捣乱鱼吃掉*/
                let maxjuli = 250;
                /*设置被抓到的鱼与捣乱鱼的距离为200像素以内时可以被捣乱鱼吃掉*/
                for (var a = 0; a < this.catchFish.length; a++) {
                    if (GameData.puTongyuId.indexOf(this.catchFish[a].id) != -1 && (this.catchFish[a].id != 7 && this.catchFish[a].id != 8)) {
                        if (this.catchFish[a].parent) {
                            var pos11 = this.catchFish[a].parent.convertToWorldSpaceAR(this.catchFish[a].getPosition());
                            /*将point2的坐标转换成屏幕坐标*/
                            var _pos1 = this.node.convertToNodeSpaceAR(pos11);
                            var obj = this.catchFish[a];
                            var _dis = this.getDistance(_pos1, this.point1.node);
                            if (_dis > minjuli) {
                                for (var b = 0; b < Arr.length; b++) {
                                    var _obj = Arr[b];
                                    var _dis1 = this.getDistance(_pos1, _obj);
                                    if (_dis1 > 150 && _dis1 < maxjuli && !_obj.catch) {
                                        var _rota = _obj.getChildByName("Allfish").rotation;
                                        var type;
                                        if (_rota > -270 && _rota > -90) {
                                            type = 4
                                        } else {
                                            type = 1
                                        }
                                        var end_p = _obj.getPosition();
                                        var start_p = _pos1;
                                        var z_p = this.getzhongxian(_pos1, _obj, 100, type);
                                        var bezier = [
                                            {"x": start_p.x, "y": start_p.y},
                                            {"x": z_p.x, "y": z_p.y},
                                            {"x": end_p.x, "y": end_p.y},
                                        ];
                                        _obj.moveStop = true;
                                        _obj.catch = true;
                                        var _x = _pos1.x;
                                        var _y = _pos1.y;
                                        var that = this;
                                        this.restAngle(_obj, end_p, start_p);
                                        _obj.runAction(
                                            cc.sequence(
                                                cc.callFunc(function () {
                                                    _obj.getChildByName("Allfish").getChildByName("fishtype").getChildByName("fish").getComponent(dragonBones.ArmatureDisplay).playAnimation("CC", 0)
                                                }),
                                                cc.moveTo(0.3, _x, _y),
                                                cc.callFunc(function () {
                                                    _obj.getChildByName("Allfish").getChildByName("fishtype").getChildByName("fish").getComponent(dragonBones.ArmatureDisplay).playAnimation("CY", 1)
                                                    fishfactory.poolFish(obj);
                                                    for (var c = 0; c < that.catchFish.length; c++) {
                                                        if (obj == that.catchFish[c]) {
                                                            that.catchFish.splice(c, 1);
                                                        }
                                                    }
                                                }),
                                                cc.delayTime(0.2),
                                                cc.callFunc(function () {
                                                    _obj.getChildByName("Allfish").getChildByName("fishtype").getChildByName("fish").getComponent(dragonBones.ArmatureDisplay).playAnimation("YD", 1)
                                                    _obj.catch = false;
                                                    that.initBackMove(_obj, bezier,);
                                                })
                                            )
                                        );
                                        break;
                                    }
                                }
                            }
                        } else {
                            this.catchFish.splice(a, 1);
                            a--;
                        }
                    } else {
                        this.catchFish.splice(a, 1);
                        a--;
                    }
                }
            }
        }
    },
    /*
    * desc: 对所有鱼进行逐帧运动
    * author: <zhouzhiwei>
    * @param: dt 每一帧的时间
    * date: 2018/11/12
    */
    AllfishMove(dt) {
        this.ceshitime += dt;
        if (this.ceshitime > 15) {
            this.ceshitime = 0;
            this.gameWave++;
            this.CreateNextWave();
            // console.log("**********进入下一波了********************", this.gameWave)
            /*进入下一波了*/
        }
        // for (var a = 0; a < this.fishArr.length; a++) {
        //     let sp = this.fishArr[a];
        //     if (sp.eatMove) {
        //         sp.eatTime += dt;
        //         if (sp.eatback) {
        //             if (sp.eatTime <= sp.eat_time) {
        //                 let position1 = sp.eatArr.getPoint(sp.eatTime);
        //                 var curPoint = sp.getPosition();  //获取鱼当前坐标
        //                 this.restAngle(sp, sp.E_end_point, curPoint);
        //                 sp.E_end_point = curPoint;  //保存当前的坐标给下一轮刷新使用
        //                 sp.position = position1;
        //             } else {
        //                 sp.eatMove = false;
        //                 sp.moveStop = false;
        //             }
        //         }
        //         else {
        //             if (this.particle2.node.scaleX == 0) {
        //                 var _rota = this.fishArr[a].rotation;
        //                 var type;
        //                 if (_rota > -270 && _rota > -90) {
        //                     type = 4
        //                 } else {
        //                     type = 1
        //                 }
        //                 var end_p = this.fishArr[a].getPosition();
        //                 /*当前鱼的位置*/
        //                 var start_p = this.fishArr[a].end_p;
        //                 /*鱼去鱼饵  鱼饵的位置*/
        //                 var z_p = this.getzhongxian(end_p, start_p, 100, type);
        //                 var bezier = [
        //                     {"x": end_p.x, "y": end_p.y},
        //                     {"x": z_p.x, "y": z_p.y},
        //                     {"x": start_p.x, "y": start_p.y},
        //                 ];
        //                 // this.fishArr[a].end_p=this.fishArr[a].getPosition();
        //                 this.initeatMove(this.fishArr[a], bezier);
        //                 sp.eatback = true;
        //                 sp.iseat=false;
        //             } else {
        //                 sp.rotation = -90;
        //                 sp.position = this.particle2.node.getPosition();
        //             }
        //         }
        //     }
        //     if (sp.backMove) {
        //         sp.backTime += dt;
        //         if (sp.backTime <= sp.back_time) {
        //             let position1 = sp.backArr.getPoint(sp.backTime);
        //             var curPoint = sp.getPosition();  //获取鱼当前坐标
        //             this.restAngle(sp, sp.B_end_point, curPoint);
        //             sp.B_end_point = curPoint;  //保存当前的坐标给下一轮刷新使用
        //             sp.position = position1;
        //         } else {
        //             sp.backMove = false;
        //             sp.moveStop = false;
        //         }
        //         break;
        //     }
        //     if (!sp.moveStop) {
        //         sp.runTime += dt;
        //         if (sp.id != 12 && sp.id != 13 && sp.id != 14) {
        //             if (sp.runTime <= sp.time) {
        //                 let position1 = sp.moveArr.getPoint(sp.runTime);
        //                 var curPoint = sp.getPosition();  //获取鱼当前坐标
        //                 this.restAngle(sp, sp.lastPoint, curPoint);
        //                 sp.lastPoint = curPoint;  //保存当前的坐标给下一轮刷新使用
        //                 sp.position = position1;
        //             } else {
        //                 sp.movidx++;
        //                 if (sp.movidx < sp.moveMaxidx) {
        //                     this.getMovePath(sp, sp.moveid);
        //                 } else {
        //                     this.anginMove(sp);
        //                 }
        //             }
        //         }
        //     }
        // }
    }
    ,
    initeatMove(obj, bezier) {
        obj.eatback = false;
        obj.eatTime = 0;
        obj.eat_time = 3;
        obj.eatArr = [];
        obj.E_end_point = obj.getPosition();
        obj.eatArr = Bezier(bezier, 100, obj.eat_time);
    }
    ,
    /*
    * desc:  当鱼有脱离原轨道时 初始化鱼回到原轨道的信息
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/12/13
    */
    initBackMove(obj, bezier) {
        obj.backTime = 0;
        obj.back_time = 3;
        obj.backArr = [];
        obj.B_end_point = obj.getPosition();
        obj.backArr = Bezier(bezier, 100, obj.back_time);
        obj.backMove = true;
    },
    /*
    * desc: 更改鱼游动时头的角度
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/12/13
    */
    restAngle(sp1, lastPoint, curPoint) {
        var sp = sp1.getChildByName("Allfish");
        var K = Math.atan(Math.abs((curPoint.y - lastPoint.y) / (curPoint.x - lastPoint.x)));
        var _jiaodu = K * 180 / 3.14159265;
        if (lastPoint.x <= curPoint.x) {
            if (lastPoint.y <= curPoint.y) {
                /* 第一象限 */
                // console.log("第一象限", _jiaodu);
                sp.rotation = (180 - _jiaodu);
                // sp.angle = -(180 - _jiaodu);
                sp.scaleY = -1;
            } else {
                /* 第四象限 */
                // console.log("第四象限", _jiaodu);
                sp.rotation = (-180 + _jiaodu);
                // sp.angle = -(-180 + _jiaodu);
                sp.scaleY = -1;
            }
        }
        else {
            if (lastPoint.y <= curPoint.y) {
                /* 第二象限 */
                // console.log("第二象限", _jiaodu);
                sp.rotation = (_jiaodu);
                // sp.angle = -(_jiaodu);
                sp.scaleY = 1;
            } else {
                /* 第三象限 */
                // console.log("第三象限", K * 180 / 3.14159265);
                sp.rotation = (-_jiaodu);
                // sp.angle = -(-_jiaodu);
                sp.scaleY = 1;
            }
        }
    },
    /*
    * desc: 获取鱼的移动路径
    * author: <zhouzhiwei>
    * @param: sp 表示的是鱼对象
    * @param: moveid 表示的是该移动轨迹的那一段路径
    * date: 2018/11/12
    */
    getMovePath(sp, moveid) {
        sp.time = 5;
        sp.moveArr = [];
        sp.moveid = moveid;
        sp.moveMaxidx = GameData.moveData[moveid].length;
        sp.runTime = 0;
        let bezier1 = GameData.moveData[moveid][sp.movidx];
        sp.moveArr = Bezier(bezier1, 100, sp.time);
        if (sp.movidx == 0) {
            sp.lastPoint = cc.v2(GameData.moveData[moveid][sp.movidx][0].x, GameData.moveData[moveid][sp.movidx][0].y);
        }
    }
    ,
    CreateNextWave() {
        if (this.gameWave > 10) {
            this.gameWave = 1;
        }
        for (var a = 0; a < this.waveData[this.gameWave].length; a++) {
            let _sp = this.waveData[this.gameWave][a];
            this.createfish(_sp.id - 1, 2);
        }
        // console.log("下一波鱼的", this.waveData, "第几波", this.gameWave)
    }
    ,
    anginMove(sp) {
        /*回收旧鱼   创建规则内的新鱼*/
        let _fishbe = sp.moveid;
        // console.log("moveid", _fishbe);
        if (parseInt(_fishbe.slice(10)) < 11) {
            this.CreateNewFish();
        }
        this.removeObjWithArr(this.fishArr, sp);
        fishfactory.poolFish(sp);
    }
    ,
    /*
    * desc: 当鱼被捕时 会随机创建一个条新鱼
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/11/23
    */
    CreateNewFish() {
        let _Arr = [];
        for (let a = 0; a < this.fristIdArr[0].length; a++) {
            _Arr.push(this.fristIdArr[0][a]);
        }
        for (let a = 0; a < this.fristIdArr[1].length; a++) {
            _Arr.push(this.fristIdArr[1][a]);
        }
        if (_Arr.length > 0) {
            var _num = Math.floor(Math.random() * (_Arr.length));
            this.createfish(_Arr[_num]);
        }
    }
    ,
    /*
    * desc: 初始化第一波鱼的创建
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/11/22
    */
    initFish() {
        let _Arr = [6, 7, 8];
        // let _Arr = [0, 0, 0];
        // let _Arr = [1, 1, 1];
        /*随机的6——8条普通鱼*/
        let _idx = Math.floor(Math.random() * _Arr.length);

        let fishNum = _Arr[_idx];
        /*鱼的数量*/
        if (this.unlocklevel >= 2) {  /*从总鱼中  分配这个3中鱼的个数*/
            this.getlist(fishNum, 3)
        } else if (this.unlocklevel == 1) {  /*从总鱼中  分配这个2中鱼的个数*/
            this.getlist(fishNum, 2)
        } else {  /*从总鱼中  分配这个1 中鱼的个数*/
            this.getlist(fishNum, 1)
        }
    },
    getlist(Allnum, num) {
        let _Arrid = [];
        this.changelist(Allnum, num, _Arrid);
        // console.log("随机鱼数", Allnum, "\n 分成几组", num, "\n分组情况", _Arrid);
        for (var a = 0; a < _Arrid.length; a++) {
            for (var b = 0; b < _Arrid[a]; b++) {
                let _id = this.unlocklevel - a;
                this.fristIdArr[0].push(_id);
                this.createfish(_id);
            }
        }
        var _num = Math.floor(Math.random() * 3 + 1);
        for (var a = 0; a < _num; a++) {
            this.fristIdArr[1].push(this.unlocklevel + 1);
            this.createfish(this.unlocklevel + 1);
        }
        // this.fristIdArr[1].push(16);
        // this.createfish(7);
        // this.createfish(7);
        // this.createfish(7);
        // this.createfish(1);
        // this.createfish(1);
        // this.createfish(1);
        // this.createfish(16);
        // this.createfish(1);
        // this.createfish(1);
    },
    /*
    *
    * 随机分配几种鱼的不同数量 鱼的数量不为0 算法
    * list:要分组的数组
    * num:分成的组数
    * */
    changelist(list, num, Alllist) {
        if (num == 1) {
            Alllist.push(list);
            return Alllist;
        } else {
            let selectnum = list - num;
            let n = Math.floor(Math.random() * selectnum + 1);
            let _list = list - n;
            Alllist.push(n);
            num--;
            this.changelist(_list, num, Alllist)
        }
    },
    /**
     * 获取生产那些鱼
     * @param id  鱼id
     * @param type 判断是否是特殊鱼
     * @param type1 判断是否是新鱼  如果是新鱼则需要打开新鱼提示
     */
    createfish(id, type, type1) {
        /*  初始化 生命 的数据*/
        var _x = Math.floor(Math.random() * 1130 - 565);
        fishfactory.createfish(id, _x, 500, that.fishprefab, that.node, that.fishArr, type);
    },

    /*
    * desc:
    * author: <zhouzhiwei>
    * @param:
    * id: 11,//"补给鱼1",
           min: 2,
           max: 7,
           created: 0,
           global: 2
    * date: 2018/11/23
    */
    initwave() {
        this.waveData = [];
        this.waveData = GameData.putongfish.concat();
        for (var a = 0; a < GameData.teshufish.length; a++) {
            let _Arr = [];
            let id = GameData.teshufish[a].id;
            let min = GameData.teshufish[a].min;
            let max = GameData.teshufish[a].max;
            let global = GameData.teshufish[a].global;
            let _num = max - min + 1;
            this.changegroup(_num, global, _Arr);
            // console.log("id", id, "第", a, "条鱼分布", _Arr);
            _Arr = this.shuffle(_Arr);
            for (var b = 0; b < _Arr.length; b++) {
                if (_Arr[b] > 0) {
                    let _obj = {
                        id: id,
                        min: _Arr[b],
                        max: _Arr[b]
                    };
                    var _wave = this.waveData[b + min].concat(_obj);
                    this.waveData[b + min] = _wave;
                }
            }
        }
    }
    ,
    shuffle(o) { //v1.0
        for (var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x) ;
        return o;
    }
    ,
    /*
    * desc: 在不同组中分配不同的鱼  可以容许组中鱼的数量为 0  算法
    * author: <zhouzhiwei>
    * @param: group 分为几组
    *  Allnum  总个数
    *  AllArr 存储结果
    * date: 2018/11/22
    */
    changegroup(group, Allnum, AllArr) {
        if (AllArr.length == group) {

        } else {
            if (Allnum <= 0) {
                for (var a = 0; a < group; a++) {
                    if (AllArr.length < group) {
                        AllArr.push(0);
                    } else {
                        break;
                    }
                }

            } else {
                let _num = Math.floor(Math.random() * (Allnum + 1));
                Allnum = Allnum - _num;
                AllArr.push(_num);
                this.changegroup(group, Allnum, AllArr);
            }
        }
    },
    /*
    * desc:  获取pos2 在pos1的那个象限
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/11/28
    */
    getxianxian(pos1, pos2, _angle) {
        if (this.isdaojuClick == 0) {
            let max = 50;
            let min = -80;
            if (this.bulletType == 2) {
                min = -65
            }


            if (pos2.x > pos1.x) {
                if (pos2.y > pos1.y) {
                    if (_angle <= -min) {
                        return _angle;
                    } else {
                        return -min;
                    }
                } else {
                    if (_angle < -max) {
                        return -max;
                    } else {
                        return _angle;
                    }
                }
            } else {
                if (pos2.y > pos1.y) {
                    return -min;
                } else {
                    return -max;
                }
            }
        } else {
            if (pos2.y < pos1.y) {
                return -90;
            } else {
                if (_angle > 45) {
                    return -135;
                } else {
                    return -_angle - 90;
                }
            }
        }

    }
    ,
    /*把数组中的对象元素从数组删除
    * desc:
    * author: <zhouzhiwei>
    * @param:
    * date: 2018/11/20
    */
    removeObjWithArr(_arr, _obj) {
        var length = _arr.length;
        for (var i = 0; i < length; i++) {
            if (_arr[i] == _obj) {
                _arr.splice(i, 1); //删除下标为i的元素
                break;
            }
        }
    },

    addcoinAct(x, y, num) {
        this.getlocakdata = GameData.getlocalData("fishData");
        var _coin;
        if (GameData.addcoinAct.size() > 0) {
            _coin = GameData.addcoinAct.get();
        } else {
            _coin = cc.instantiate(this.addcoin);
        }
        _coin.position = cc.v2(x, y);
        _coin.parent = this.node;
        _coin.getComponent("addmoney").init(num);
    },
    onDestroy() {
        GameData.GameLayer = null;
        // console.log("onDestroy")
    }
});
